package com.example.hao.lateralspreadstodelete;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class LateralSpreadsToDelete extends View {

    private static final String TAG = "LateralSpreadsToDelete";
    private int widthPixels;
    //默认宽高
    private static int DEFAULT_WIDTH;
    private static final int DEFAULT_HEIGHT = 200;
    private GestureDetector gestureDetector;
    private int measuredWidth;
    private int measuredHeight;
    private Paint rectPaint;

    private Rect rect;
    private int maxDistance;

    public LateralSpreadsToDelete(Context context) {
        super(context);
    }

    public LateralSpreadsToDelete(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        gestureDetector = new GestureDetector(getContext(), simpleOnGestureListener);
        gestureDetector.setIsLongpressEnabled(false);
        widthPixels = getContext().getResources().getDisplayMetrics().widthPixels;
        DEFAULT_WIDTH = widthPixels + 500;
        initPaint();
    }

    private void initPaint() {
        rectPaint = new Paint();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int specModeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int specSizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int resultWidth = specSizeWidth;

        switch (specModeWidth) {
            case MeasureSpec.EXACTLY://match_parent 或 500dp(固定值)
                resultWidth = specSizeWidth;
                break;
            case MeasureSpec.AT_MOST://wrap_content
                resultWidth = DEFAULT_WIDTH;
                break;
            case MeasureSpec.UNSPECIFIED:
                break;
        }

        int specModeHeight = MeasureSpec.getMode(heightMeasureSpec);
        int specSizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int resultHeight = specSizeHeight;

        switch (specModeHeight) {
            case MeasureSpec.EXACTLY://match_parent 或 500dp(固定值)
                resultHeight = specSizeHeight;
                break;
            case MeasureSpec.AT_MOST://wrap_content
                resultHeight = DEFAULT_HEIGHT;
                break;
            case MeasureSpec.UNSPECIFIED:
                break;
        }

        setMeasuredDimension(resultWidth, resultHeight);
        initData();
    }


    private void initData() {
        measuredWidth = getMeasuredWidth();
        measuredHeight = getMeasuredHeight();

        maxDistance = measuredWidth - widthPixels;


        itemR = widthPixels;
        left = itemR;
        right = widthPixels + maxDistance;
    }

    int itemL = 0;
    int itemR = 0;
    int left = 0;
    int right = 0;

    boolean isClick ;
    @Override
    protected void onDraw(Canvas canvas) {

        rectPaint.setStyle(Paint.Style.FILL);
        rectPaint.setColor(Color.BLUE);
        Log.d(TAG, "onDraw: " + widthPixels);
        canvas.drawRect(itemL, 0, itemR, measuredHeight, rectPaint);
        rectPaint.setColor(Color.RED);
        canvas.drawRect(left, 0, right, measuredHeight, rectPaint);


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        gestureDetector.onTouchEvent(event);
        if (!isClickable()){
            int action = event.getAction();
            switch (action){
                case MotionEvent.ACTION_UP:
                    if (itemL<0) {
                        if (itemL>-maxDistance/2){
                            itemL = 0;
                            itemR = widthPixels;
                            left = widthPixels;
                            right = measuredWidth;
                        }else {
                            itemL = -maxDistance;
                            itemR = widthPixels - maxDistance;
                            left = widthPixels -maxDistance;
                            right = measuredWidth - maxDistance;
                        }
                    }
                    invalidate();
                    break;
            }
        }
        return true;
    }

    GestureDetector.SimpleOnGestureListener simpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return super.onSingleTapUp(e);
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            isClick = true;
            itemL -= distanceX;
            itemR -= distanceX;
            left -= distanceX;
            right -= distanceX;
            if (itemL >= 0) {
                itemL = 0;
                itemR = widthPixels;
                left = widthPixels;
                right = widthPixels + maxDistance;
            } else {
                itemL = itemL <= -maxDistance ? -maxDistance : itemL;
                itemR = widthPixels - itemR >= maxDistance ? widthPixels - maxDistance : itemR;
                left = left <= widthPixels - maxDistance ? widthPixels - maxDistance : left;
                right = right <= measuredWidth - maxDistance ? measuredWidth - maxDistance : right;
            }


            invalidate();
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.d(TAG, "onFling: "+velocityX);

            float abs = Math.abs(velocityX);
            if (velocityX<0){
                if (abs>=4000){
                    itemL= -maxDistance;
                    itemR = widthPixels - maxDistance;
                    left = widthPixels - maxDistance;
                    right = measuredWidth - maxDistance;
                }
            }else {
                itemL= 0;
                itemR = widthPixels;
                left = widthPixels;
                right = measuredWidth;
            }

            invalidate();

            return true;
        }
    };
}
